package fr.uniscol;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import fr.uniscol.model.DefaultUser;
import fr.uniscol.model.IUniscolUser;
import fr.uniscol.mvc.SessionExpireeException;
import fr.uniscol.view.Alert;
import fr.uniscol.view.CustomInternalResourceViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.ViewResolver;

@Controller
public class UniscolMainController {

	public static final Logger log = Logger.getLogger(UniscolMainController.class);
	
	@Autowired
	private MessageSource messageSource;
	
	/*public IUniscolUser getUtilisateur(HttpSession session) {
		IUniscolUser user = (IUniscolUser) session.getAttribute("utilisateur");
		if(user == null){
			user = new DefaultUser();
			session.setAttribute("utilisateur", user);
		}
		return user;
	}*/
	
    @Autowired
    protected ViewResolver viewResolver;
    
	@ExceptionHandler(SessionExpireeException.class)
	public String handleSessionExpiree(SessionExpireeException e){
		return "redirect:session.expiree";
	}

	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllExceptions(Exception ex){
		
		log.fatal(ex);
		ex.printStackTrace();
		
		return new ModelAndView("erreur", "erreur", ex);
	}
    
    @ModelAttribute("utilisateur")
    protected IUniscolUser getUtilisateur(HttpSession session){
        IUniscolUser utilisateur = (IUniscolUser) session.getAttribute("utilisateur");
        if(utilisateur == null){
            utilisateur = new DefaultUser();
            session.setAttribute("utilisateur", utilisateur);
        }
        return utilisateur;
    }
	
	public void alertInfo(String code, String defaultMessage, Model model){
		model.addAttribute("alert", Alert.createAlertInfo(code, defaultMessage));
	}
    
    public void alertInfo(String code, String defaultMessage, HttpSession session){
        session.setAttribute("alert", Alert.createAlertInfo(code, defaultMessage));
    }
	
	public void alertSuccess(String code, String defaultMessage, Model model){
		model.addAttribute("alert", Alert.createAlertSuccess(code, defaultMessage));
	}
    
    public void alertSuccess(String code, String defaultMessage, HttpSession session){
		session.setAttribute("alert", Alert.createAlertSuccess(code, defaultMessage));
	}
	
	public void alertError(String code, String defaultMessage, Model model){
		model.addAttribute("alert", Alert.createAlertError(code, defaultMessage));
	}
    
    public void alertError(String code, String defaultMessage, HttpSession session){
		session.setAttribute("alert", Alert.createAlertError(code, defaultMessage));
	}
    
    public void setLayout(String layoutName){
        ((CustomInternalResourceViewResolver) viewResolver).setTemplate(layoutName);
    }
}
